package com.ruoyi.core.controller;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.AppType;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.core.service.ClientOperationService;
import com.ruoyi.core.vo.HeartbeatMessage;
import com.ruoyi.core.vo.JsPluginLoginDTO;
import com.ruoyi.core.vo.ProductUserLoginCallback;
import com.ruoyi.core.vo.ProductUserLogoutCallback;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @Description:
 * @Company: 北京卡拉卡尔股份技术有限公司
 * @Author: Darren Ho
 * @Date: 10/10/2020 15:31
 */
@Api("客户端用户操作接口")
@RestController
public class ClientOperationController {

    @Autowired
    private ClientOperationService clientOperationService;

    /**
     * 修改client-product-user
     */
    @ApiOperation("内置账号登录回调")
    @PostMapping("/callback")
    public AjaxResult productUserLoginCallback(@RequestBody ProductUserLoginCallback productUserLoginCallback) {

        try {

            return AjaxResult.success(clientOperationService.productUserLoginCallback(productUserLoginCallback));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

    }

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @ApiOperation("客户端登录")
    @PostMapping("/client/login")
    public AjaxResult login(@RequestBody LoginBody loginBody) {
        try {
            AjaxResult ajax = AjaxResult.success();
            // 生成令牌
            String token = clientOperationService.login(loginBody);
            ajax.put(Constants.TOKEN, token);
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(HttpStatus.BAD_REQUEST, e.getMessage());
        }

    }

    @ApiOperation("客户端登录")
    @PostMapping("/client/loginByJsPlugin")
    public AjaxResult loginByPluginJs(@RequestBody LoginBody loginBody){
        JsPluginLoginDTO jsPluginLoginDTO = clientOperationService.loginByPluginJs(loginBody);
        return AjaxResult.success(jsPluginLoginDTO);
    }

    /**
     * 获取客户端用户信息
     *
     * @return 用户信息
     */
    @ApiOperation("客户端登录获取当前用户信息")
    @GetMapping("/client/getInfo")
    public AjaxResult getInfo() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", loginUser.getUser());
        ajax.put("deviceId", loginUser.getUuid());
        return ajax;
    }

    /**
     * 修改client-product-user
     */
    @ApiOperation("客户端心跳机制")
    @PostMapping("/heartbeat")
    public AjaxResult heartbeat(@Valid @RequestBody HeartbeatMessage heartbeatMessage) {

        try {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            if (!heartbeatMessage.getPayload().getClientUserId().equals(loginUser.getUser().getUserId())) {
                throw new CustomException("传入参数中当前登录用户信息与真实登录用户信息不一致");
            }
            return AjaxResult.success(clientOperationService.handleHeartbeatMessage(heartbeatMessage));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }

    }

    /**
     * 修改client-product-user
     */
    @ApiOperation("内置用户退出回调")
    @PostMapping("/product-user/logout-callback")
    public AjaxResult productUserLogoutCallback(@Valid @RequestBody ProductUserLogoutCallback productUserLogoutCallback) {

        try {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            if (!productUserLogoutCallback.getClientUserId().equals(loginUser.getUser().getUserId())) {
                throw new CustomException("传入参数中当前登录用户信息与真实登录用户信息不一致");
            }
            return AjaxResult.success(clientOperationService.productUserLogoutCallback(productUserLogoutCallback));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }

    }

    /**
     * 刷新jungle-scout网站登录-cookie
     */
    @ApiOperation("刷新jungle-scout网站登录-cookie")
    @GetMapping("/jungle-scout-cookie-refresh/{productId}")
    public AjaxResult jungleScoutCookieRefresh(@PathVariable("productId") Long productId,
                                               @RequestParam(name = "productUserIds", required = false) Long[] productUserIds) {

        return AjaxResult.success(clientOperationService.jungleScoutCookieRefresh(productId, productUserIds));

    }

    /**
     * merchant-scout网站登录-cookie
     */
    @ApiOperation("亚马逊网站 cookie 刷新开打开")
    @GetMapping("/merchant-word-cookie-refresh-on/{productId}")
    public AjaxResult merchantWordCookieRefreshOnOff(@PathVariable("productId") Long productId,
                                                     @RequestParam(name = "productUserIds", required = false) Long[] productUserIds) {

        return AjaxResult.success(clientOperationService.merchantWordCookieRefreshOnOff(productId, productUserIds));

    }

    /**
     * 刷新js plugin网站登录-cookie
     */
    @ApiOperation("刷新js plugin网站登录-cookie")
    @GetMapping("/js-plugin-cookie-refresh/{productId}")
    public AjaxResult jsPluginCookieRefresh(@PathVariable("productId") Long productId,
                                               @RequestParam(name = "productUserIds", required = false) Long[] productUserIds) {

        return AjaxResult.success(clientOperationService.jsPluginCookieRefresh(productId, productUserIds));

    }


    /**
     * 刷新卖家精灵网站登录-cookie
     */
    @ApiOperation("刷新卖家精灵网站登录-cookie")
    @GetMapping("/elves-cookie-refresh/{productId}")
    public AjaxResult elvesCookieRefresh(@PathVariable("productId") Long productId) {

        return AjaxResult.success(clientOperationService.cookieRefresh(productId, AppType.ELVES));

    }
}
